package reflection;

import annotation.FieldStudent;
import annotation.TableStudent;
import pojo.Student;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.Arrays;

/**
 * 反射操作注解
 */
public class Test06 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException {
        final Class<?> stuClass = Class.forName("pojo.Student");

        //通过反射获得注解的值
        final Annotation[] annotations = stuClass.getAnnotations();
        Arrays.stream(annotations).forEach(System.out::println);

        //获得注解的value值
        final TableStudent tableStudent = stuClass.getAnnotation(TableStudent.class);
        String value = tableStudent.value();
        System.out.println(value);

        //获得对应属性上的注解，以及注解中的值
        final Field name = stuClass.getDeclaredField("name");
        final FieldStudent fieldStudent = name.getAnnotation(FieldStudent.class);
        System.out.println(fieldStudent.columnName());
        System.out.println(fieldStudent.type());
        System.out.println(fieldStudent.length());
    }
}


